package com.zzedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzedu.common.cache.CacheNameContansts;
import com.zzedu.common.utils.StringUtils;
import com.zzedu.domain.ZzUploadFile;
import com.zzedu.dto.ZzUploadFileDTO;
import com.zzedu.enmuns.FileRelationTypeEnum;
import com.zzedu.mapper.ZzUploadFileMapper;
import com.zzedu.service.IZzUploadFileService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * 资源管理Service业务层处理
 *
 * @author Mr.Wang
 * @date 2025-01-30
 */
@Service
public class ZzUploadFileServiceImpl extends ServiceImpl<ZzUploadFileMapper, ZzUploadFile> implements IZzUploadFileService {

    @Override
    public LambdaQueryWrapper<ZzUploadFile> buildQuery(ZzUploadFileDTO zzUploadFile) {
        LambdaQueryWrapper<ZzUploadFile> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.like(StringUtils.isNotBlank(zzUploadFile.getOldFileName()), ZzUploadFile::getOldFileName, zzUploadFile.getOldFileName());
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(zzUploadFile.getUrl()), ZzUploadFile::getUrl, zzUploadFile.getUrl());
        lambdaQueryWrapper.like(StringUtils.isNotBlank(zzUploadFile.getFileName()), ZzUploadFile::getFileName, zzUploadFile.getFileName());
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(zzUploadFile.getFileExt()), ZzUploadFile::getFileExt, zzUploadFile.getFileExt());
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(zzUploadFile.getSourceModel()), ZzUploadFile::getSourceModel, zzUploadFile.getSourceModel());
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(zzUploadFile.getRelationId()), ZzUploadFile::getRelationId, zzUploadFile.getRelationId());
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(zzUploadFile.getRelationType()), ZzUploadFile::getRelationType, zzUploadFile.getRelationType());
        lambdaQueryWrapper.eq(null != zzUploadFile.getGmtCreate(), ZzUploadFile::getGmtCreate, zzUploadFile.getGmtCreate());
        lambdaQueryWrapper.eq(null != zzUploadFile.getGmtModify(), ZzUploadFile::getGmtModify, zzUploadFile.getGmtModify());
        lambdaQueryWrapper.eq(null != zzUploadFile.getDeleted(), ZzUploadFile::getDeleted, zzUploadFile.getDeleted());

        lambdaQueryWrapper.orderByDesc(ZzUploadFile::getId);
        return lambdaQueryWrapper;
    }

    @Override
    public boolean removeRelation(String relationId, FileRelationTypeEnum fileRelationTypeEnum, List<Long> excludefileIdList) {
        return remove(Wrappers.lambdaQuery(ZzUploadFile.class)
                .eq(ZzUploadFile::getRelationType, fileRelationTypeEnum.getCode())
                .eq(ZzUploadFile::getRelationId, relationId)
                .notIn(ZzUploadFile::getId, excludefileIdList)
        );
    }

    @Override
    public boolean updateRelation(String relationId, List<Long> idList) {
        return update(Wrappers.lambdaUpdate(ZzUploadFile.class)
                .set(ZzUploadFile::getRelationId, relationId)
                .in(ZzUploadFile::getId, idList)
        );
    }

    @Override
    @Cacheable(value = CacheNameContansts.UPLOAD_FILE_CACHE, key = "'upload_file:'+#p0")
    public ZzUploadFile getById(Serializable id) {
        return super.getById(id);
    }
}